home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / codecs / mccomponent / fadescreen.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  5.6 KB  |  189 lines

  1. /*
  2.     File:        FadeScreen.c
  3.  
  4.     Contains:    code snippet for fading a screen of any depth in or out.
  5.  
  6.     Written by: John Wang    
  7.  
  8.     Copyright:    Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/18/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 03/14/94    JW        Re-Created for Universal Headers.
  21.  
  22. */
  23.  
  24. #ifdef THINK_C
  25. #define        applec
  26. #endif
  27.  
  28. #include    <Memory.h>
  29. #include    <QuickDraw.h>
  30. #include    <Devices.h>
  31. #include    <Video.h>
  32. #include    <LowMem.h>
  33.  
  34. #include    "FadeScreen.h"
  35.  
  36. /* ------------------------------------------------------------------------- */
  37.  
  38. /*
  39.     Description:    Call this routine to fade any screen in or out.
  40.  
  41.     Format Params:    
  42.         Name            Usage    Description/Assumptions
  43.         ----            ----    -----------------------
  44.         myDevice        PI        Pass in the device handle to the screen.
  45.         inOut            PI        Pass in whether to fade in or out.  TRUE to fade out (black).
  46.                                 FALSE to fade back in.
  47.         smoothness        PI        Number of steps to fade in or out.  A good number is 60.
  48.         
  49.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  50.     
  51.     Error Handling:    If it can't allocate the color table necessary, then it returns without
  52.                     doing anything.
  53.  
  54.     Special Notes:    xxx put other comments here xxx
  55.  
  56. */
  57.  
  58. void FadeInOut(GDHandle myDevice, Boolean inOut, short smoothness)
  59. {
  60.     GDHandle        saveDevice;
  61.     
  62.     saveDevice = GetGDevice();
  63.     SetGDevice(myDevice);
  64.  
  65.     if ( (**myDevice).gdType == directType ) {
  66.         DirectFadeInOut(myDevice, inOut, smoothness);
  67.     } else {
  68.         IndexedFadeInOut(myDevice, inOut, smoothness);
  69.     }
  70.  
  71.     SetGDevice(saveDevice);
  72. }
  73.  
  74. /* ------------------------------------------------------------------------- */
  75.  
  76. /*    Internal routines used by fadeInOut.    */
  77.  
  78. void DirectFadeInOut(GDHandle myDevice, Boolean inOut, short smoothness)
  79. {
  80.     VDSetEntryRecord    setEntriesRec;    /* DirectSetEntries information */
  81.     Ptr                    csPtr;            /* Passes setEntriesRect to the video card */
  82.     short                index;            /* Index into the color table */
  83.     short                iter;
  84.     CTabHandle            animClut;        /* Color table we’re using for animation. */
  85.     short                pixelSize;
  86.     long                colorComp;
  87.     RGBColor            *thisRGB;
  88.     long                now;
  89.     
  90.     animClut = (CTabHandle) NewHandle(sizeof(ColorTable) + 255 * sizeof(ColorSpec));
  91.     if (animClut == nil)
  92.         goto bail;
  93.         
  94.     (**animClut).ctSeed = GetCTSeed ();
  95.     (**animClut).ctFlags = 0;
  96.     pixelSize = (**((**myDevice).gdPMap)).pixelSize;
  97.     if ( pixelSize == 32 )
  98.         (**animClut).ctSize = 255;
  99.     else if ( pixelSize == 16 )
  100.         (**animClut).ctSize = 31;
  101.     else
  102.         goto bail;
  103.  
  104.     for ( iter = 0; iter < smoothness; iter++ ) {
  105.         now = TickCount();
  106.         for ( index = 0; index <= (**animClut).ctSize; index++ ) {
  107.             if (pixelSize == 32)
  108.                 colorComp = (index << 8) + index;
  109.             else if (pixelSize == 16)
  110.                 colorComp = (index << 11) + (index << 6) + (index << 1) + (index >> 4);
  111.             if (inOut)
  112.                 colorComp = (colorComp * (smoothness - iter)) / smoothness;
  113.             else
  114.                 colorComp = (colorComp * iter) / smoothness;
  115.             thisRGB = &((**animClut).ctTable[index].rgb);
  116.             thisRGB->red = (short) colorComp;
  117.             thisRGB->green = (short) colorComp;
  118.             thisRGB->blue = (short) colorComp;
  119.         }
  120.         
  121.         //    Set the video card’s DACs
  122.         setEntriesRec.csTable = (**animClut).ctTable;
  123.         setEntriesRec.csStart = 0;
  124.         setEntriesRec.csCount = (**animClut).ctSize;
  125.         csPtr = (Ptr) &setEntriesRec;
  126.         Control ((**myDevice).gdRefNum, cscDirectSetEntries, (Ptr) &csPtr);
  127.         
  128.         //    Wait
  129.         while ( now == TickCount() );
  130.     }
  131.     
  132. bail:
  133.     if (animClut != nil)
  134.         DisposeHandle((Handle) animClut);
  135. }
  136.  
  137. void IndexedFadeInOut(GDHandle myDevice, Boolean inOut, short smoothness)
  138. {
  139.     VDSetEntryRecord    setEntriesRec; /* DirectSetEntries information */
  140.     Ptr                    csPtr;         /* Passes setEntriesRect to the video card */
  141.     short                index;         /* Index into the color table */
  142.     short                iter;
  143.     CTabHandle            animClut, sourceColorTB;
  144.     RGBColor            *thisRGB, *sourceRGB;
  145.     long                now;
  146.     
  147.     animClut = (CTabHandle) NewHandle (sizeof (ColorTable) + 255 * sizeof (ColorSpec));
  148.     if ( animClut == nil )
  149.         goto bail;
  150.         
  151.     sourceColorTB = (**((**myDevice).gdPMap)).pmTable;
  152.  
  153.     (**animClut).ctSeed = GetCTSeed();
  154.     (**animClut).ctFlags = 0;
  155.     (**animClut).ctSize = (**sourceColorTB).ctSize;
  156.  
  157.     //    Set the video card’s DACs        
  158.     for ( iter = 0; iter < smoothness; iter++ ) {
  159.         now = TickCount();
  160.         for ( index = 0; index <= (**animClut).ctSize; index++ ) {
  161.             thisRGB = &((**animClut).ctTable[index].rgb);
  162.             sourceRGB = &((**sourceColorTB).ctTable[index].rgb);
  163.             if (inOut) {
  164.                 thisRGB->red = (sourceRGB->red * (smoothness - iter)) / smoothness;
  165.                 thisRGB->green = (sourceRGB->green * (smoothness - iter)) / smoothness;
  166.                 thisRGB->blue = (sourceRGB->blue * (smoothness - iter)) / smoothness;
  167.             } else {
  168.                 thisRGB->red = (sourceRGB->red * iter) / smoothness;
  169.                 thisRGB->green = (sourceRGB->green * iter) / smoothness;
  170.                 thisRGB->blue = (sourceRGB->blue * iter) / smoothness;
  171.             }
  172.         }
  173.         
  174.         //    Set the video card’s DACs
  175.         setEntriesRec.csTable = (**animClut).ctTable;
  176.         setEntriesRec.csStart = 0;
  177.         setEntriesRec.csCount = (**animClut).ctSize;
  178.         csPtr = (Ptr) &setEntriesRec;
  179.         Control ((**myDevice).gdRefNum, cscSetEntries, (Ptr) &csPtr);
  180.         
  181.         //    Wait
  182.         while ( now == TickCount() );
  183.     }
  184.     
  185. bail:
  186.     if (animClut != nil)
  187.         DisposeHandle((Handle) animClut);
  188. }
  189.